<?php

namespace App\Http\Controllers\Admin;

use App\Http\Config\ErrorCode;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AnnualLeaveController extends BaseController
{
    public function __construct(Request $request){
        parent::__construct($request);
        try {
            if (empty(DB::select('SHOW TABLES LIKE "annual_leave"'))) {
                DB::statement("CREATE TABLE `annual_leave` (
   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
   `mall_id` int(10) unsigned DEFAULT 0 COMMENT '所属商城ID',
   `day_count` int(2) DEFAULT 0 COMMENT '年假天数',
   `year_add` int(2) DEFAULT 0 COMMENT '每年增加',
   `max_day` int(2) DEFAULT 0 COMMENT '最大天数',
   `rule` varchar(30) DEFAULT 0 COMMENT '规则',
   `day_lapse` int(2) DEFAULT 0 COMMENT '失效天数',
   PRIMARY KEY (`id`),
  key (`mall_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COMMENT='年假配置'");
            }
        }catch (\Exception $e){}
    }

    public function getInfo(){
        if ($this->returnData['code'] > 0){
            return $this->returnData;
        }
        $this->returnData['data'] = DB::table("annual_leave")->where("mall_id", request()->mallData["id"])->first();
        if(!$this->returnData['data']){
            DB::table('annual_leave')->insert([
                "day_count" => 0,
                "year_add" => 0,
                "max_day" => 0,
                "rule" => 0,
                "day_lapse" =>0,
                "mall_id" => request()->mallData["id"]
            ]);
            $this->returnData['data'] = DB::table("annual_leave")->first();
        }
        return $this->return_result($this->returnData);
    }

    public function update(Request $request,$id){
        if ($this->returnData['code'] > 0){
            return $this->returnData;
        }
        $res = DB::table('annual_leave')->where("id", $id)->update([
            "day_count" => $request->post("day_count", 0),
            "year_add" => $request->post("year_add", 0),
            "max_day" => $request->post("max_day", 0),
            "rule" => $request->post("rule", 0),
            "day_lapse" => $request->post("day_lapse", 0),
        ]);
        if(!$res){
            $this->returnData = ErrorCode::$admin_enum['fail'];
            $this->returnData['msg'] = "更新失败";
            return $this->return_result($this->returnData);
        }
        $this->returnData['msg'] = "更新成功";
        return $this->return_result($this->returnData);
    }
}
